﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MUD.Lib.Objects.Users;
using MUD.Lib.Utils;
using MUD.Lib.Objects;

namespace MUD.Lib.Utils
{
    internal abstract class VerbProcessor : MudObject
    {
        public delegate bool CommandHandler(User user, string command);
        protected User.ReceiveHandler ThisHandler;
        protected Dictionary<string, CommandHandler> Verbs = new Dictionary<string, CommandHandler>();

        public VerbProcessor()
        {
            ThisHandler = new User.ReceiveHandler(ProcessCommand);
        }


        public bool ProcessCommand(User user, string command)
        {
            try
            {
                if(Verbs.Keys.Contains(command.NthWord(0)))
                    return Verbs[command.NthWord(0)](user, command);
                return false;
            }
            catch (Exception ex)
            {
                user.MasterObj.Log(ex.Message);
                return false;
            }
        }

        public void ListenToUser(User user)
        {
            user.OnReceive += ThisHandler;
        }

        public void StopListeningToUser(User user)
        {
            user.OnReceive -= ThisHandler;
        }

        public void AddVerb(string verb, CommandHandler handler)
        {
            if (!Verbs.Keys.Contains(verb))
                Verbs.Add(verb, handler);
        }

        public void RemoveVerb(string verb)
        {
            if (Verbs.Keys.Contains(verb))
                Verbs.Remove(verb);
        }
    }
}
